const { flow } = require("mobx-miniprogram");

// pages/Comment/Comment.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:'',
        type:'',
        hotOffset:0,
        newOffset:1,
        limit:20,
        hotComments:[],
        newComments:[],
        isHot:false,
        isLoading:false,
        cursor:0,
        show:true
    },

    /*
        type
        0: 歌曲
        1: mv
        2: 歌单
        3: 专辑
        4: 电台
        5: 视频
    */
    /**
     * 生命周期函数--监听页面加载
     */
    // 获取热门评论
    async getHotComment(){
        this.setData({
            isLoading:true
        })
        wx.showLoading({
          title: 'loading...',
        })
        const res  = await wx.p.request({
          url: `${wx.root}/comment/hot`,
          data:{
              id:this.data.id,
              type:this.data.type,
              limit:this.data.limit,
              offset:this.data.hotOffset*this.data.limit
          }
        })
        // console.log(res.data);
        this.setData({
            hotComments:[...this.data.hotComments,...res.data.hotComments],
            hotOffset:this.data.hotOffset+1
        })

        wx.hideLoading({
          success: (res) => {
              this.setData({
                  isLoading:false
              })
          },
        })
    },
    // 改变评论选择
    changeType(){
        this.setData({
            isHot:!this.data.isHot
        })
    },
    // 获取最新评论
    async getNewComment(){
        this.setData({
            isLoading:true
        })
        wx.showLoading({
          title: 'loading...',
        })
        const res = await wx.p.request({
            url:`${wx.root}/comment/new`,
            data:{
                id:this.data.id,
                type:this.data.type,
                sortType:3,
                cursor:this.data.cursor,
                pageNo:this.data.newOffset,
            }
        })
        this.setData({
            newComments:[...this.data.newComments,...res.data.data.comments],
            cursor:res.data.data.cursor,
            newOffset:this.data.newOffset+1,
        })
        wx.setNavigationBarTitle({
            title: '评论('+res.data.data.totalCount+')',
        })
         wx.hideLoading({
          success: (res) => {
              this.setData({
                  isLoading:false
              })
          },
        })
    },
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: '评论',
          })
        this.setData({
            id:options.id,
            type:options.type
        })
        
        this.getHotComment()
        this.getNewComment()
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.isLoading == false){
          if(this.data.isHot)  this.getHotComment() 
          else this.getNewComment()
        }

           
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})